package com.huawei.hwclouds.lubanops.demo.web.schedule;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import com.huawei.hwclouds.lubanops.demo.common.client.HttpCallResult;
import com.huawei.hwclouds.lubanops.demo.common.client.HttpClient;
import com.huawei.hwclouds.lubanops.demo.common.json.JsonUtil;
import com.huawei.hwclouds.lubanops.demo.common.model.HttpHeaderMap;
import com.huawei.hwclouds.lubanops.demo.common.util.ConfigServiceUtil;
import com.huawei.hwclouds.lubanops.demo.order.client.OrderServiceClient;
import com.huawei.hwclouds.lubanops.demo.product.client.ProductHeaderFactory;
import com.huawei.hwclouds.lubanops.demo.shoppingcart.client.ShoppingCartHeaderFactory;
import com.huawei.hwclouds.lubanops.demo.shoppingcart.client.ShoppingCartServiceClient;

@Configuration
@EnableScheduling
public class WebNormalScheduleConfig {
    private static final HttpClient CLIENT = ConfigServiceUtil
        .getHttpClient(
            "web-client.properties", ShoppingCartHeaderFactory.class);
    private static final String CONFIG_SERVER_ADDRESS = ConfigServiceUtil.getConfigServerAddress(
        "web-client.properties", ProductHeaderFactory.class);
    private static final String PREFIX = CONFIG_SERVER_ADDRESS + "/apm2/demo/web/";

    @Scheduled(cron = "0/2 * * * * ?")
    public void queryProduct() {
        Map<String, Integer> param = new HashMap<>();
        param.put("pageNumber", 1);
        param.put("pageSize", 20);
        CLIENT.sendPostJSON(PREFIX + "/product/info", HttpHeaderMap.getDefaultHeader(), JsonUtil.toJSONString(param));
    }

    @Scheduled(cron = "0/5 * * * * ?")
    public void addShoppingCart() {
        Map<String, Object> param = new HashMap<>();
        param.put("img", "xxx.xx");
        param.put("shop", "成都旗舰店");
        param.put("userId", "1");
        param.put("title", "牙膏");
        param.put("merchandiseId", 1);
        param.put("num", 1);
        param.put("price", 12.5);
        CLIENT.sendPostJSON(PREFIX + "/add/shopping/cart", HttpHeaderMap.getDefaultHeader(),
            JsonUtil.toJSONString(param));
    }

    @Scheduled(cron = "0/2 * * * * ?")
    public void queryShoppingCarts() {
        Map<String, Object> param = new HashMap<>();
        param.put("userId", "1");
        param.put("pageNumber", 1);
        param.put("pageSize", 20);
        CLIENT.sendPostJSON(PREFIX + "/shopping/info", HttpHeaderMap.getDefaultHeader(), JsonUtil.toJSONString(param));
    }

    @Scheduled(cron = "0/15 * * * * ?")
    public void settleAccounts() {
        Map<String, Object> param = new HashMap<>();
        param.put("userId", "1");
        param.put("pageNumber", 1);
        param.put("pageSize", 5);
        HttpCallResult httpCallResult = ShoppingCartServiceClient.queryShoppingCarts(JsonUtil.toJSONString(param));
        CLIENT.sendPostJSON(PREFIX + "/settle/account", HttpHeaderMap.getDefaultHeader(), httpCallResult.getContent());
    }

    @Scheduled(cron = "0/40 * * * * ?")
    public void deleteOrders() {
        OrderServiceClient.deleteOrders("1");
    }

    @Scheduled(cron = "0 0 * * * ?")
    public void clearData() {
        OrderServiceClient.clear();
        ShoppingCartServiceClient.clear();
    }

    @Scheduled(cron = "0/2 * * * * ?")
    public void error() {
        CLIENT.sendGet(PREFIX + "/shopping/error/1", HttpHeaderMap.getDefaultHeader());
    }

    @Scheduled(cron = "0/5 * * * * ?")
    public void query() {
        CLIENT.sendGet(PREFIX + "/product/1", HttpHeaderMap.getDefaultHeader());
    }

}
